<template>
  <div class="search-page">
    <!-- 搜索区域 -->
    <div class="search-container">
      <van-search
          v-model="searchText"
          placeholder="请输入搜索的标签"
          wrap-with-form
          show-action
          @search="onSearch"
          @cancel="onCancel"
          class="search-input"
          shape="round"
      />
    </div>
    
    <!-- 已选标签区域 -->
    <div class="selected-tags-container">
      <van-divider content-position="left" class="section-divider">
        <van-icon name="bookmark" size="16" />
        已选标签
      </van-divider>
      
      <div v-if="activeIds.length === 0" class="empty-tags">
        <van-icon name="info" size="16" />
        <span>请选择标签</span>
      </div>
      
      <div v-else class="selected-tags">
        <van-tag 
          v-for="tag in activeIds" 
          :key="tag"
          closeable 
          size="medium" 
          type="primary" 
          class="selected-tag"
          @close="doClose(tag)"
        >
          {{ tag }}
        </van-tag>
      </div>
    </div>
    
    <!-- 标签选择区域 -->
    <div class="tag-selector-container">
      <van-divider content-position="left" class="section-divider">
        <van-icon name="search" size="16" />
        选择标签
      </van-divider>
      
      <van-tree-select
          v-model:active-id="activeIds"
          v-model:main-active-index="activeIndex"
          :items="tagList"
          class="tag-tree-select"
      />
    </div>
    
    <!-- 搜索按钮 -->
    <div class="search-button-container">
      <van-button 
        type="primary" 
        block 
        class="search-button"
        @click="doSearchResult"
        :disabled="activeIds.length === 0"
      >
        <van-icon name="search" size="16" />
        开始搜索
      </van-button>
    </div>
  </div>
</template>

<style scoped>
.search-page {
  min-height: 100vh;
  background: transparent;
  padding: 16px;
  padding-bottom: 100px;
}

.search-container {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 16px;
  margin-bottom: 20px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  animation: fadeInUp 0.6s ease-out;
}

.selected-tags-container {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 20px;
  margin-bottom: 20px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  animation: fadeInUp 0.8s ease-out;
}

.tag-selector-container {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 20px;
  margin-bottom: 20px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  animation: fadeInUp 1s ease-out;
}

.section-divider {
  margin: 0 0 16px 0;
  color: #2c3e50;
  font-weight: 600;
  font-size: 16px;
}

.section-divider .van-icon {
  color: #667eea;
  margin-right: 8px;
}

.empty-tags {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #7f8c8d;
  font-size: 14px;
  padding: 20px;
  text-align: center;
  justify-content: center;
  background: rgba(102, 126, 234, 0.05);
  border-radius: 12px;
  border: 1px dashed rgba(102, 126, 234, 0.2);
}

.selected-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.selected-tag {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 20px;
  padding: 6px 12px;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.selected-tag:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.tag-tree-select {
  background: transparent;
}

.search-button-container {
  position: fixed;
  bottom: 100px;
  left: 16px;
  right: 16px;
  z-index: 100;
}

.search-button {
  height: 52px;
  border-radius: 26px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  box-shadow: 0 8px 24px rgba(102, 126, 234, 0.3);
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.search-button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 12px 32px rgba(102, 126, 234, 0.4);
}

.search-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

/* 深度选择器优化 Vant 组件样式 */
:deep(.van-search__content) {
  background: rgba(248, 249, 250, 0.8);
  border-radius: 24px;
  border: 1px solid rgba(102, 126, 234, 0.2);
}

:deep(.van-search__content:focus-within) {
  border-color: #667eea;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.1);
}

:deep(.van-field__control) {
  color: #2c3e50;
  font-size: 14px;
}

:deep(.van-field__control::placeholder) {
  color: #bdc3c7;
}

:deep(.van-tree-select__content) {
  background: transparent;
}

:deep(.van-tree-select__nav) {
  background: rgba(248, 249, 250, 0.8);
  border-radius: 12px;
  border: 1px solid rgba(102, 126, 234, 0.1);
}

:deep(.van-tree-select__item) {
  color: #2c3e50;
  font-weight: 500;
  transition: all 0.3s ease;
}

:deep(.van-tree-select__item--active) {
  color: #667eea;
  background: rgba(102, 126, 234, 0.1);
}

:deep(.van-tree-select__item:hover) {
  background: rgba(102, 126, 234, 0.05);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .search-page {
    padding: 12px;
    padding-bottom: 90px;
  }
  
  .search-container,
  .selected-tags-container,
  .tag-selector-container {
    padding: 16px;
    margin-bottom: 16px;
  }
  
  .search-button-container {
    bottom: 90px;
    left: 12px;
    right: 12px;
  }
  
  .search-button {
    height: 48px;
    font-size: 15px;
  }
  
  .selected-tag {
    font-size: 13px;
    padding: 5px 10px;
  }
}
</style>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';

/**
 * 搜索过滤
 */
const router = useRouter();
const searchText = ref('');

const doSearchResult = () => {
  router.push({
    path: '/user/list',
    query: {
      tags: activeIds.value
    }
  });
};

const originalList = [
  {
    text: '浙江',
    children: [
      { text: '杭州', id: '杭州' },
      { text: '温州', id: '温州' },
      { text: '宁波', id: '宁波' },
    ],
  },
  {
    text: '江苏',
    children: [
      { text: '南京', id: '南京' },
      { text: '无锡', id: '无锡' },
      { text: '徐州', id: '徐州' },
      { text: '无tian', id: '无tian' },
      { text: '徐hu', id: '徐hu' },
    ],
  },
  { text: '福建' },
];

let tagList = ref(originalList);

const onSearch = () => {
  tagList.value = originalList.map(parentTag => {
    // 创建父标签的深拷贝
    const tempParentTag = { ...parentTag };

    // 处理children（安全处理undefined情况）
    tempParentTag.children = parentTag.children
        ? [...parentTag.children].filter(child =>
            child.text.includes(searchText.value)
        )
        : [];

    return tempParentTag; // 必须返回新对象
  });
};

const onCancel = () => {
  searchText.value = '';
  tagList.value = originalList;
};

// 选择标签
const activeIds = ref([]);
const activeIndex = ref(0);

// 删除已选标签
const doClose = (tag) => {
  activeIds.value = activeIds.value.filter(item => {
    return item !== tag;
  });
};
</script>